package main

import (
	"fmt"
	"os"

	"github.com/xuri/excelize/v2"
)

func main() {
	// 先删除文件
	fileName := "Book1.xlsx"
	_, err := os.Stat(fileName)
	if err != nil && !os.IsNotExist(err) {
		fmt.Println(err)
		return
	}
	if !os.IsNotExist(err) {
		if err := os.Remove(fileName); err != nil {
			fmt.Println(err)
			return
		}
	}

	f := excelize.NewFile()
	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()
	for idx, row := range [][]interface{}{
		{nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
		{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
	} {
		cell, err := excelize.CoordinatesToCellName(1, idx+1)
		if err != nil {
			fmt.Println(err)
			return
		}
		if err := f.SetSheetRow("Sheet1", cell, &row); err != nil {
			fmt.Println(err)
			return
		}
	}

	if err := f.SaveAs(fileName); err != nil {
		fmt.Println(err)
	}
}
